package com.spiro.test.net.tcp.ssl.bidirectional;

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;

/**
 * Created by tz0643 on 2016/6/17.
 *
 * JVM options:
 * -Djavax.net.ssl.keyStore=C:\Users\tz0643\Desktop\jsse\sslserverkeys
 * -Djavax.net.ssl.keyStorePassword=123456
 * -Djavax.net.ssl.trustStore=C:\Users\tz0643\Desktop\jsse\sslservertrust
 * -Djavax.net.ssl.trustStorePassword=123456
 */
public class SSLTcpServerWithJVMOptions {

    private SSLServerSocket serverSocket = null;


    public void guest (Socket socket) {
        Thread t = new Thread(new ServiceHandler(socket));
        t.start();
    }

    public void start(int port) throws IOException {

        try {
            SSLServerSocketFactory factory
                    = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
            serverSocket = (SSLServerSocket) factory.createServerSocket(port);
        } catch (IOException e) {
            throw e;
        }

        while (true) {
            guest(serverSocket.accept());
            System.out.println("Guest client");
        }
    }


    class ServiceHandler implements Runnable {

        private Socket socket = null;
        private BufferedReader reader = null;
        private PrintWriter writer = null;

        public ServiceHandler(Socket socket) {
            this.socket = socket;
        }

        public Socket getSocket() {
            return socket;
        }

        @Override
        public void run() {

            try {
                reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
                writer = new PrintWriter(this.socket.getOutputStream());

                String line = null;
                while ((line = reader.readLine()) != null) {

                    if ("close".equals(line)) {
                        break;
                    }

                    System.out.println("c -> " + line);
                    writer.println("Received, t - " + new Date().toString());
                    writer.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                    }
                }
                if (writer != null) {
                    writer.close();
                }

                if (this.socket != null) {
                    try {
                        this.socket.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
    }

    public static void main(String[] args) {

        SSLTcpServerWithJVMOptions tcpServer = new SSLTcpServerWithJVMOptions();
        try {
            tcpServer.start(9000);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
